<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PostViewModel
 * 文章视图模型
 * @author yihua
 */

namespace Common\Model;

use Think\Model\ViewModel;

class PostViewModel extends ViewModel {

    /**
     * 加载文章列表
     */
    protected $tableName = 'post';
    public $viewFields = array(
        'post' => array('*', 'type', '_type' => 'LEFT'),
        'post_item' => array('group_concat(distinct item_id)' => ' item_ids', '_on' => 'post.post_id=post_item.post_id', '_type' => 'LEFT'),
        'post_tag' => array('_on' => 'post.post_id=post_tag.post_id','_type'=>'LEFT'),
        'tag' => array('group_concat(distinct tag_name)' => ' tag_names', '_on' => 'tag.tag_id=post_tag.tag_id')
    );

    public function setView($view) {
        if (empty($view) || $view == 'info') {
            return $this;
        } elseif ($view == 'list') {
            $this->viewFields = array(
                'post' => array('post_id', 'post_title', 'post_content', 'type', '_type' => 'LEFT'),
                'post_item' => array('group_concat(distinct post_item.item_id)' => ' item_ids', '_on' => 'post.post_id=post_item.post_id', '_type' => 'LEFT'),
                'post_tag' => array('group_concat(distinct post_tag.tag_id)' => ' tag_ids', '_on' => 'post.post_id=post_tag.post_id', '_type' => 'LEFT'),
                'item' => array('group_concat(distinct item_name)' => ' item_names', '_on' => 'item.item_id=post_item.item_id', '_type' => 'LEFT'),
                'tag' => array('group_concat(distinct tag_name)' => ' tag_names', '_on' => 'tag.tag_id=post_tag.tag_id')
            );
            return $this;
        }
    }

}
